package com.tuozixuan.jmh.demo;

import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Measurement(iterations = 3, time = 100, timeUnit = TimeUnit.MILLISECONDS)
@Warmup(iterations = 3, time = 100, timeUnit = TimeUnit.MILLISECONDS)
public class JmhDemo_Benchmark_Exception {
    
    @Benchmark
    public void exception() throws InterruptedException {
        // 如果从方法体抛出异常，则此基准测试的JMH运行将突然结束，JMH将向下运行下一个基准测试列表。
        TimeUnit.SECONDS.sleep(3);
        throw new RuntimeException("demo exception");
    }
    
    @Benchmark
    public void noException() throws InterruptedException {
        TimeUnit.SECONDS.sleep(3);
    }
    
    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(JmhDemo_Benchmark_Exception.class.getSimpleName())
                .forks(1)
                .build();
        new Runner(opt).run();
    }
}
